import Command from "../enum/Command.js"
import BufferTool from "../tool/BufferTool.js"
import Ntlm from "../tool/ntlm/ntlm.js"

export default class SessionSetupAgree {

  static command = Command.SESSION_SETUP
  static structure = {
    request: [
      ['StructureSize', 2, 25], //固定值 25 实际长度是24，不知为何定为25
      ['Flags', 1, 0],  //: If the client implements the SMB 3.x dialect family, this field MUST be set to combination of zero or more of the following values. Otherwise, it MUST be set to 0.
      ['SecurityMode', 1, 1],
      ['Capabilities', 4, 0],
      ['Channel', 4, 0], //This field MUST NOT be used and MUST be reserved. The client MUST set this to  0, and the server MUST ignore it on receipt.
    
      ['SecurityBufferOffset', 2, 88], //The offset, in bytes, from the beginning of the SMB 2 Protocol header to the security buffer.
    
      ['SecurityBufferLength', 2],
      ['PreviousSessionId', 8, 0],
      ['Buffer', 'SecurityBufferLength', (connectObj)=>{
        if (!connectObj.nonce) {
          return Ntlm.createType1Message()
        }
        return Ntlm.createType3Message(
          connectObj.nonce,
          connectObj.userName,
          connectObj.password,
         
        )
      }],
    ],
  
    response: [
      ['StructureSize', 2],
      ['SessionFlags', 2],
      ['SecurityBufferOffset', 2],
      ['SecurityBufferLength', 2],
      ['Buffer', 'SecurityBufferLength'],
    ],
  }


  constructor(){

  }

  static onResponse(connectObj, message){
    if (!connectObj.sessionId) {
      console.log(message.headerStruc.SessionId)
      connectObj.sessionId = message.headerStruc.SessionId;
      console.log("收到的sessionid", message.headerStrucFormat.SessionId)
      connectObj.nonce = Ntlm.decodeType2Message(message.bodyStruc.Buffer);
    }

 
  }

}